package com.fengycx.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fengycx.constants.Constants;
import com.fengycx.constants.GlobalExceptionCode;
import com.fengycx.feign.AppLoginApi;
import com.fengycx.mapper.CustomerMapper;
import com.fengycx.pojo.domain.Customer;
import com.fengycx.pojo.domain.CustomerSummary;
import com.fengycx.pojo.domain.CustomerWallet;
import com.fengycx.pojo.param.AppLoginApiParam;
import com.fengycx.result.R;
import com.fengycx.service.ICustomerService;
import com.fengycx.service.ICustomerSummaryService;
import com.fengycx.service.ICustomerWalletService;
import com.fengycx.template.WechatTemplate;
import com.fengycx.utils.AssertUtil;
import com.fengycx.utils.BitStatesUtil;
import com.fengycx.utils.NameUtil;
import java.math.BigDecimal;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 * <p>
 * 客户 服务实现类
 * </p>
 *
 * @author Cino
 * @since 2024-02-23
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {


    @Autowired
    private WechatTemplate wechatTemplate;

    @Autowired
    private AppLoginApi appLoginApi;

    @Autowired
    private ICustomerSummaryService summaryService;

    @Autowired
    private ICustomerWalletService walletService;

    @Override
    @Transactional
    public void code2OpenId(String code) {

        String openId = wechatTemplate.getOpenId(code);

        //判断用户是否注册过
        Customer customer = super.getOne(new LambdaQueryWrapper<Customer>().eq(Customer::getOpenId, openId));
        AssertUtil.isNull(customer, GlobalExceptionCode.PARAM_PHONE_EXIST);
        Customer appCustomer = saveAppCustomer(openId);

        //初始化主键相同的其他关联表
        initCustomerSummery(appCustomer.getId());
        initCustomerWallet(appCustomer.getId());

        //保存login对象
        AppLoginApiParam appLoginApiParam = new AppLoginApiParam();
        appLoginApiParam.setId(appCustomer.getId());
        appLoginApiParam.setType(2);
        appLoginApiParam.setAdmin(false);
        appLoginApiParam.setAvatar(Constants.AVATAR_URL);
        appLoginApiParam.setNickName(appCustomer.getName());
        appLoginApiParam.setOpenId(openId);
        appLoginApiParam.setPhone(appCustomer.getPhone());
        R<Boolean> result = appLoginApi.appLogin(appLoginApiParam);
        AssertUtil.isTrue(result.isSuccess(),GlobalExceptionCode.SERVICE_ERROR);
        
    }

    private void initCustomerWallet(Long customerId) {
        CustomerWallet customerWallet = new CustomerWallet();
        customerWallet.setId(customerId);
        customerWallet.setAmount(new BigDecimal("0"));
        //生成随机密码，阿里云 发短信给用户 TODO
        String pwd = RandomUtil.randomString(8);
        customerWallet.setPassword(pwd);
        customerWallet.setVersion(0);
        customerWallet.setCreateTime(new Date());
        walletService.save(customerWallet);
    }

    private void initCustomerSummery(Long customerId) {
        CustomerSummary customerSummary = new CustomerSummary();
        customerSummary.setId(customerId);
        customerSummary.setTotalOrders(0);
        customerSummary.setTotalFine(new BigDecimal("0"));
        customerSummary.setVoucherNumbers(0);
        customerSummary.setCancelOrders(0);
        summaryService.save(customerSummary);
    }


    private Customer saveAppCustomer(String openid) {
        Customer customer = new Customer();
        String s2 = RandomUtil.randomNumbers(1);
        String nums = RandomUtil.randomNumbers(9);
        customer.setPhone("1"+s2+nums);
        customer.setSex(1);
        customer.setName(NameUtil.getName());
        customer.setCreateTime(new Date());
        customer.setOpenId(openid);
        customer.setBitState(BitStatesUtil.OP_PHONE);
        super.save(customer);
        return customer;
    }
}
